---
keywords: ""
description: "Swift documentation for 'PartialRangeFrom'"
title: "PartialRangeFrom"
layout: "default"
root: "/v5.1"
---

<div class="intro-declaration"><code class="language-swift">struct PartialRangeFrom</code></div><p>A partial interval extending upward from a lower bound.</p>
<table class="standard"><tr><th id="inheritance">Inheritance</th><td><code class="inherits">RangeExpression</code></td></tr></table><div class="discussion comment"><p>You create <code>PartialRangeFrom</code> instances by using the postfix range operator
(postfix <code>...</code>).</p>
<pre><code class="language-swift">let atLeastFive = 5...
</code></pre>
<p>You can use a partial range to quickly check if a value is contained in a
particular range of values. For example:</p>
<pre><code class="language-swift">atLeastFive.contains(4)
// false
atLeastFive.contains(5)
// true
atLeastFive.contains(6)
// true
</code></pre>
<p>You can use a partial range of a collection's indices to represent the
range from the partial range's lower bound up to the end of the
collection.</p>
<pre><code class="language-swift">let numbers = [10, 20, 30, 40, 50, 60, 70]
print(numbers[3...])
// Prints &quot;[40, 50, 60, 70]&quot;
</code></pre>
<h4>Using a Partial Range as a Sequence</h4>
<p>When a partial range uses integers as its lower and upper bounds, or any
other type that conforms to the <code>Strideable</code> protocol with an integer
stride, you can use that range in a <code>for</code>-<code>in</code> loop or with any sequence
method that doesn't require that the sequence is finite. The elements of
a partial range are the consecutive values from its lower bound continuing
upward indefinitely.</p>
<pre><code class="language-swift">func isTheMagicNumber(_ x: Int) -&gt; Bool {
    return x == 3
}

for x in 1... {
    if isTheMagicNumber(x) {
        print(&quot;\(x) is the magic number!&quot;)
        break
    } else {
        print(&quot;\(x) wasn't it...&quot;)
    }
}
// &quot;1 wasn't it...&quot;
// &quot;2 wasn't it...&quot;
// &quot;3 is the magic number!&quot;
</code></pre>
<p>Because a <code>PartialRangeFrom</code> sequence counts upward indefinitely, do not
use one with methods that read the entire sequence before returning, such
as <code>map(_:)</code>, <code>filter(_:)</code>, or <code>suffix(_:)</code>. It is safe to use operations
that put an upper limit on the number of elements they access, such as
<code>prefix(_:)</code> or <code>dropFirst(_:)</code>, and operations that you can guarantee
will terminate, such as passing a closure you know will eventually return
<code>true</code> to <code>first(where:)</code>.</p>
<p>In the following example, the <code>asciiTable</code> sequence is made by zipping
together the characters in the <code>alphabet</code> string with a partial range
starting at 65, the ASCII value of the capital letter A. Iterating over
two zipped sequences continues only as long as the shorter of the two
sequences, so the iteration stops at the end of <code>alphabet</code>.</p>
<pre><code class="language-swift">let alphabet = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;
let asciiTable = zip(65..., alphabet)
for (code, letter) in asciiTable {
    print(code, letter)
}
// &quot;65 A&quot;
// &quot;66 B&quot;
// &quot;67 C&quot;
// ...
// &quot;89 Y&quot;
// &quot;90 Z&quot;
</code></pre>
<p>The behavior of incrementing indefinitely is determined by the type of
<code>Bound</code>. For example, iterating over an instance of
<code>PartialRangeFrom&lt;Int&gt;</code> traps when the sequence's next value would be
above <code>Int.max</code>.</p>
</div><h3>Initializers</h3><div id="init-00a2b987552997ee43208aca83ac6ae6" class="declaration"><a class="toggle-link" href="#comment-init-00a2b987552997ee43208aca83ac6ae6">init init(_:)</a> <span class="required">Required</span><div class="comment collapse in" id="comment-init-00a2b987552997ee43208aca83ac6ae6"><h4>Declaration</h4><code class="language-swift">@inlinable public init(_ lowerBound: Bound)</code></div></div><h3>Instance Variables</h3><div id="lowerBound-88584f2bd264a57a30e5142795244349" class="declaration"><a class="toggle-link" href="#comment-lowerBound-88584f2bd264a57a30e5142795244349">let lowerBound</a> <span class="required">Required</span><div class="comment collapse in" id="comment-lowerBound-88584f2bd264a57a30e5142795244349"><h4>Declaration</h4><code class="language-swift">let lowerBound: Bound</code></div></div><h3>Instance Methods</h3><div id="contains-6f3567afe6aa63234ecc71820249f524" class="declaration"><a class="toggle-link" href="#comment-contains-6f3567afe6aa63234ecc71820249f524">func contains(_ element: Bound) -> Bool</a> <span class="required">Required</span><div class="comment collapse in" id="comment-contains-6f3567afe6aa63234ecc71820249f524"><p>Returns a Boolean value indicating whether the given element is contained
within the range expression.</p>
<ul>
<li>Parameter element: The element to check for containment.</li>
</ul>
<h4>Declaration</h4><code class="language-swift">@inlinable public func contains(_ element: Bound) -> Bool</code></div></div><div id="relative_to-2949b1d9b740a6772c83bb21cba0c554" class="declaration"><a class="toggle-link" href="#comment-relative_to-2949b1d9b740a6772c83bb21cba0c554">func relative(to collection: C) -> Range&lt;Bound></a> <span class="required">Required</span><div class="comment collapse in" id="comment-relative_to-2949b1d9b740a6772c83bb21cba0c554"><p>Returns the range of indices described by this range expression within
the given collection.</p>
<p>You can use the <code>relative(to:)</code> method to convert a range expression,
which could be missing one or both of its endpoints, into a concrete
range that is bounded on both sides. The following example uses this
method to convert a partial range up to <code>4</code> into a half-open range,
using an array instance to add the range's lower bound.</p>
<pre><code class="language-swift">let numbers = [10, 20, 30, 40, 50, 60, 70]
let upToFour = ..&lt;4

let r1 = upToFour.relative(to: numbers)
// r1 == 0..&lt;4
</code></pre>
<p>The <code>r1</code> range is bounded on the lower end by <code>0</code> because that is the
starting index of the <code>numbers</code> array. When the collection passed to
<code>relative(to:)</code> starts with a different index, that index is used as the
lower bound instead. The next example creates a slice of <code>numbers</code>
starting at index <code>2</code>, and then uses the slice with <code>relative(to:)</code> to
convert <code>upToFour</code> to a concrete range.</p>
<pre><code class="language-swift">let numbersSuffix = numbers[2...]
// numbersSuffix == [30, 40, 50, 60, 70]

let r2 = upToFour.relative(to: numbersSuffix)
// r2 == 2..&lt;4
</code></pre>
<p>Use this method only if you need the concrete range it produces. To
access a slice of a collection using a range expression, use the
collection's generic subscript that uses a range expression as its
parameter.</p>
<pre><code class="language-swift">let numbersPrefix = numbers[upToFour]
// numbersPrefix == [10, 20, 30, 40]
</code></pre>
<ul>
<li>Parameter collection: The collection to evaluate this range expression
in relation to.</li>
</ul>
<h4>Declaration</h4><code class="language-swift">public func relative&lt;C>(to collection: C) -> Range&lt;Bound> where Bound == C.Index, C: Collection</code></div></div><h3>Type Methods</h3><div id="pattern_value-d3d1acc155fe92facb4e9662e5491d7c" class="declaration"><a class="toggle-link" href="#comment-pattern_value-d3d1acc155fe92facb4e9662e5491d7c">func ~=(pattern: Self, value: Self.Bound) -> Bool</a> <span class="required">Required</span><div class="comment collapse in" id="comment-pattern_value-d3d1acc155fe92facb4e9662e5491d7c"><h4>Declaration</h4><code class="language-swift">@inlinable public static func ~=(pattern: Self, value: Self.Bound) -> Bool</code></div></div>